package com.zg.ktcomwan.adapter

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.Navigation
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zg.core.ext.logI
import com.zg.ktcomwan.R
import com.zg.ktcomwan.databinding.ArticleItemBinding
import com.zg.ktcomwan.databinding.ProjectListItemBinding
import com.zg.ktcomwan.model.ArticleBean

/***
 * @Author:      zxd
 * @CreateDate:  2021/7/7 11:00
 * @Description: 首页文章的适配器
 */
class ArticleAdapter(private val context: Context) :
    PagingDataAdapter<ArticleBean, ArticleAdapter.ViewHolder>(ArticleDiffCallback()) {

    class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: ArticleBean, position: Int, context: Context) {
            if (TextUtils.isEmpty(item.envelopePic)) {
                val binding: ArticleItemBinding = binding as ArticleItemBinding
                binding.setArticle(item)
                binding.setContext(context)
                binding.root.setOnClickListener {
                    val bundle = Bundle()
                    bundle.putString("url", item.link)
                    bundle.putString("title", item.title)
//                    Navigation.findNavController(it).navigate(R.id.goWeb, bundle)
                    Navigation.findNavController(it).navigate(R.id.navigation_web, bundle)
                }
            } else {
                val binding: ProjectListItemBinding = binding as ProjectListItemBinding
                binding.setArticle(item)
                binding.setOwner(context as LifecycleOwner?)
                binding.setChaptername(false)
                Glide.with(context).load(item.envelopePic).into(binding.projectListIv)
                binding.root.setOnClickListener {
                    val bundle = Bundle()
                    bundle.putString("url", item.link)
                    bundle.putString("title", item.title)
//                    Navigation.findNavController(it).navigate(R.id.goWeb, bundle)
                    Navigation.findNavController(it).navigate(R.id.navigation_web, bundle)
                }
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let { holder.bindData(it, holder.layoutPosition, context) };
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            viewType,
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun getItemViewType(position: Int): Int {
        val item: ArticleBean? = getItem(position)
        //返回的是布局引用，如果是这样的话，那么onCreateViewHolder2中的viewType就是布局文件的引用
        if (TextUtils.isEmpty(item?.envelopePic)) {
            return R.layout.article_item
        } else if (!TextUtils.isEmpty(item?.envelopePic)) {
            return R.layout.project_list_item
        }
        return 0
    }
}

class ArticleDiffCallback : DiffUtil.ItemCallback<ArticleBean>() {
    override fun areItemsTheSame(oldItem: ArticleBean, newItem: ArticleBean): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: ArticleBean, newItem: ArticleBean): Boolean {
        return oldItem == newItem
    }
}